//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    img: app.globalData.imgurl,
    scrollDown: false,
    listHeight:0,
    showType: false,
    ani:{},
    typeSt:[],
    parentid:0,
    typeNd:[],
    typeNdId:null,
    keyword:"",
    exampleList: [],
    exampleNo: 0,
    styleList: [],
    styleNo: 0,
    shareid: 0,
    shareimg: 0
  },
  onLoad: function () {
    // let query = wx.createSelectorQuery();
    // query.select('#cardList').boundingClientRect(res => {
    // console.log(res);
    //   this.setData({
    //     listHeight: res.top
    //   })
    // }).exec()
    // var animation = wx.createAnimation({
    //   duration: 200,
    //   timingFunction: 'linear'
    // });
    // this.animation = animation

    

    this.getFirstType()
  },
  getFirstType(){
    var _this = this
    var exampleNo = 0
    var styleNo = 0
    wx.request({
      url: app.globalData.baseurl +'/product/ChildrenPictureClassification',
      data: {
        parentid: 1,
        keywords: ""
      },
      method: "GET",
      success (res) {
        var arr = res.data.Items
        if(arr.length){
          exampleNo = arr[0].RecPictureClassificationId
        }
        _this.setData({
          exampleList: arr,
          exampleNo,
        })
      }
    })
    wx.request({
      url: app.globalData.baseurl +'/product/ParentPictureClassification',
      data: {
        keywords: ""
      },
      method: "GET",
      success (res) {
        var arr = res.data.Items
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].RecPictureClassificationId == 1) {
            arr.splice(i,1)
            break;
          }
        }
        if(arr.length){
          styleNo = arr[0].RecPictureClassificationId
        }
        _this.setData({
          styleList: arr,
          styleNo,
        })
        _this.getSecondType()
      }
    })
  },
  getSecondType(){
    var _this = this
    wx.request({
      url: app.globalData.baseurl +'/product/ChildrenPictureClassification',
      data: {
        parentid: this.data.styleNo,
        keywords: ""
      },
      method: "GET",
      success (res) {
        _this.setData({
          typeNd: res.data.Items
        })
      },
      fail(res){
        console.log(res)
      }
    })
  },
  chooseExample(e){
    console.log(e);
    this.setData({
      exampleNo: e.target.dataset.key
    })
  },
  chooseStyle(e){
    console.log(e);
    this.setData({
      styleNo: e.target.dataset.key
    })
    this.getSecondType()
  },
  onPageScroll(e){
    // console.log(e);
    
    // if(e.scrollTop>this.data.listHeight){
    //   this.setData({
    //     scrollDown: true
    //   })
    // }else{
    //   this.setData({
    //     scrollDown: false
    //   })
    // }
  },
  toTop(){
    // wx.pageScrollTo({
    //   scrollTop: 0,
    //   duration: 500
    // })
    // this.closeTip()
  },
  // showType(){
  //   var _this = this
  //   this.animation.translate(0, 230).step()
    
  //   this.setData({
  //     showType: true,
  //   })
  //   setTimeout(()=>{
  //     _this.setData({
  //       ani: _this.animation.export()
  //     })
  //   },1)
  // },
  closeTip(){
    // this.animation.translate(0, 0).step()
    // this.setData({
    //   showType: false,
    //   ani: this.animation.export()
    // })
  },
  changtext(e){
    console.log(e);
    
    this.setData({
      keyword:e.detail.value
    })
  },
  selectType(e){
    if(this.data.typeStId != e.currentTarget.dataset.id){
      this.setData({
        typeStId:e.currentTarget.dataset.id
      })
    }else{
      this.setData({
        typeStId:0
      })
    }
    
    this.getSecondType()
  },
  search(){
    // this.getSecondType()
    wx.navigateTo({
      url:"./imgpage/imgpage?keywords=" + this.data.keyword
    })
  },
  toSeeImg(e){
    console.log(e.currentTarget.dataset.id);
    wx.navigateTo({
      url:"./imgpage/imgpage?id="+e.currentTarget.dataset.id + "&eid="+this.data.exampleNo + "&name=" +e.currentTarget.dataset.name
    })
  },
  share(e){
    // this.setData({
    //   shareid:e.currentTarget.dataset.id,
    //   shareimg:e.currentTarget.dataset.img
    // })
  },
  onShareAppMessage: function (e) {
    console.log(e);
    if(e.form == "button"){
      return {
        title: e.target.name,
        path: '/page/imgpage/imgpage?id=' + e.target.id + "&eid="+this.data.exampleNo + "&name=" + e.target.name,
        imageUrl: this.data.img + e.target.img
      }
    }
  }
})
